package edu.dali.enterprise_test.controller.auth;

import com.github.pagehelper.PageHelper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.model.vo.SysPermission;
import edu.dali.enterprise_test.model.vo.SysRole;
import edu.dali.enterprise_test.service.auth.AuthService;
import edu.dali.enterprise_test.service.auth.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleMgrController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 主界面
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "role/index";
    }

    /**
     * 获取列表
     * @return
     */
    @RequestMapping("getRoles")
    @ResponseBody
    public Pager<SysRole> getRoles(int page , int limit) {
        Dto dto = new Dto();

        String role = request.getParameter("role");
        String description = request.getParameter("description");
        if (!StringUtils.isEmpty(role))
            dto.put("role" , role);
        if (!StringUtils.isEmpty(description))
            dto.put("description" , description);
        PageHelper.startPage(page , limit);
        Pager<SysRole> p = roleService.getRoles(dto);

        return p;
    }

    @RequestMapping("detail/{roleid}")
    public String detail(@PathVariable String roleid , Model model) {
        Dto dto = new Dto();
        dto.put("roleid" , roleid);
        SysRole role = roleService.selectRole(dto);
        model.addAttribute("role" , role);
        return "role/detail";
    }

    /**
     * 添加角色
     * @return
     */
    @RequestMapping("addRole")
    public String addRole(Model model) {
        List<SysPermission> permissions = authService.getAllPermissions(null);
        model.addAttribute("permissions" , permissions);
        return "role/add";
    }

    /**
     * 添加用户
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public Map<String , String> add(String rolename , String description , String available ,@RequestParam(value = "permissions[]") Long[] permissions) {
        Map<String , String> map = new HashMap<>();

        SysRole role = new SysRole();
        role.setAvailable(true);
        role.setRole(rolename);
        role.setDescription(description);
        List<SysPermission> perms = new ArrayList<>();

        for (Long permsId : permissions) {
            SysPermission perm = new SysPermission();
            perm.setId(permsId);
            perms.add(perm);
        }
        role.setPermissions(perms);

        roleService.insertRole(role);


        map.put("code" , "0");
        map.put("msg" , "添加角色成功");
        return map;
    }

    /**
     * 更新角色信息界面
     * @return
     */
    @RequestMapping("updateRole/{rid}")
    public String updateRole(@PathVariable Long rid , Model model) {
        Dto dto = new Dto();
        dto.put("roleid" , rid);
        SysRole role = roleService.selectRole(dto);
        model.addAttribute("role" , role);

        List<SysPermission> rperm = role.getPermissions();
        List<String> perms = new ArrayList<>();
        for (SysPermission perm : rperm) {
            perms.add(perm.getPermission());
        }
        List<SysPermission> permissions = authService.getAllPermissions(null);
        model.addAttribute("permissions" , permissions);
        model.addAttribute("perms" , perms);
        return "role/update";
    }

    /**
     * 更新角色信息处理
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public Map<String , String> update(Long rid , String rolename , String description , String available ,@RequestParam(value = "permissions[]" , required = false) Long[] permissions) {
        Map<String , String> map = new HashMap<>();

        SysRole role = new SysRole();
        role.setId(rid);
        role.setRole(rolename);
        role.setDescription(description);
        role.setAvailable(true);
        List<SysPermission> perms = new ArrayList<>();
        for (Long permission : permissions) {
            SysPermission perm = new SysPermission();
            perm.setId(permission);

            perms.add(perm);
        }

        role.setPermissions(perms);
        roleService.updateRole(role);

        map.put("code" , "0");
        map.put("msg" , "修改角色信息成功");
        return map;
    }

    /**
     * 删除一个角色
     * @param rid
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public Map<String , String> delete(Long rid) {
        Map<String , String> map = new HashMap<>();

        SysRole role = new SysRole();
        role.setId(rid);
        roleService.deleteRole(role);

        map.put("code" , "0");
        map.put("msg" , "删除成功");
        return map;
    }
}
